package cn.org.zx.common.commutils;

import java.text.DateFormat; 
import java.text.ParseException; 
import java.text.SimpleDateFormat; 
import java.util.Calendar; 
import java.util.Date;
import java.util.List;

public class CompareTime {
	public static List getBetweenDays(String t1,String t2) throws ParseException{ 
		DateFormat format = new SimpleDateFormat("yyyy-MM-dd"); 
		int betweenDays = 0; 
		java.util.Date d1 = format.parse(t1); 
		java.util.Date d2 = format.parse(t2); 
		Calendar c1 = Calendar.getInstance(); 
		Calendar c2 = Calendar.getInstance(); 
		Calendar cTemp = null;
		c1.setTime(d1); 
		c2.setTime(d2); 
		
		java.util.List lstDate = new java.util.ArrayList();

		int betweenYears = c2.get(Calendar.YEAR)-c1.get(Calendar.YEAR); 
	
		betweenDays = c2.get(Calendar.DAY_OF_YEAR)-c1.get(Calendar.DAY_OF_YEAR)+1; 

		for(int i=0;i<betweenYears;i++){ 
			c1.set(Calendar.YEAR,(c1.get(Calendar.YEAR)+1)); 
			betweenDays += c1.getMaximum(Calendar.DAY_OF_YEAR); 
		} 

		for(int i=0;i<betweenDays;i++){ 
			cTemp = Calendar.getInstance();
			cTemp.setTime(d1);
			cTemp.set(5, cTemp.get(5)+i);
			//System.out.println(format.format(cTemp.getTime()));
			lstDate.add(format.format(cTemp.getTime()));
			
		} 
		
		return lstDate; 
	} 
	
	public static int getYearMonth(Date date){
        //传入日期
        Calendar calder = Calendar.getInstance();
        calder.setTime(date);//设置时间
        int year = calder.get(Calendar.YEAR);//获取年份
        int month=calder.get(Calendar.MONTH);//获取月份
        //返回年份乘以100加上月份的值，因为月份最多2位数，
        // 所以年份乘以100可以获取一个唯一的年月数值
        return year*100+month;
    }
	
	public static int getYearMonthDiff(String begin,String end){
		int ireturn = 0;
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");
		Date begindate;
		Date enddate;
	        try {
	        	begindate = df.parse(begin);
	        	enddate = df.parse(end);
	        }catch (ParseException e){
	            e.printStackTrace();
	            begindate=new Date();
	            enddate=new Date();
	            ireturn = 0;
	        }
		
		int dateOne = getYearMonth(begindate);
        int dateTwo = getYearMonth(enddate);
        if(dateOne==dateTwo){
        	ireturn=2;
            //System.out.println("两个日期的年月相等");
        }else if(dateOne>dateTwo){
        	ireturn=3;
            //System.out.println("dateOne比dateTwo的年月晚");
         }else{
        	 ireturn=4;
            //System.out.println("dateOne比dateTwo的年月早");
        }
		
		return ireturn;
	}
	

	
	public static void main(String[] args) throws ParseException{		
		String t1 = "2020-10"; 
		
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM");		
		Date begindate = df.parse(t1);
		Calendar c = Calendar.getInstance();
		c.setTime(begindate);	  
	    c.add(Calendar.MONTH, -1);
	    System.out.println(df.format(c.getTime()));
	} 

} 


